var overlay = {
	openSideBar: function(){
		if(!overlay.isSidebarOpen()){
   		toggleSidebar('viewHypertopicSidebar', true);
  		document.getElementById("sidebar").contentWindow.location = "chrome://lasuli/content/lasuliSidebar.xul";
  	}
	},

  isSidebarOpen: function(){
		return (document.getElementById("viewHypertopicSidebar").getAttribute("checked") == "true");
	},

	doSaveToWebDav: function(){
	  var currentUri = getURL() + "";
	  var stringBundle = document.getElementById("sbLasuli");
	  if(currentUri == "" || (currentUri.substring(0, "about:".length) == "about:")){
	    alert(stringBundle.getString("hypertopicOverlay.error.urlisnull"));
	    return;
	  }
	  var dav = new WebDav(currentUri);
    var docHtml = mainWindow.getBrowser().contentDocument.documentElement.innerHTML;
    var res = dav.save(docHtml);
    if(res)
      alert(stringBundle.getString("hypertopicOverlay.doSaveToWebDav.ok"));
	},

	doOpenCreateItemDialog: function(){
	  var currentUri = getURL() + "";
	  var stringBundle = document.getElementById("sbLasuli");
	  if(currentUri == "" || (currentUri.substring(0, "about:".length) == "about:")){
	    alert(stringBundle.getString("hypertopicOverlay.error.urlisnull"));
	    return;
	  }
    openDialog("chrome://lasuli/content/createItem.xul", "dlg-create-item", "centerscreen=yes, close=no");
	},

  doHighlightMenuClick: function(evt){
		var currentUri = getURL() + "";
		var stringBundle = document.getElementById("sbLasuli");
	  if(currentUri == "" || (currentUri.substring(0, "about:".length) == "about:")){
	    alert(stringBundle.getString("hypertopicOverlay.error.urlisnull"));
	    return false;
	  }

    this.openSideBar();
    debug("[hypertopicOverlay.js::doHighlightMenuClick] current Uri:" + currentUri, 3);
	  var sidebarWindow = document.getElementById("sidebar").contentWindow;

    var targetId = evt.target.getAttribute("id");
    var colorIndex = parseInt(targetId.substr(-1, 1))-1;
    var selectionPos = HT_Selection.getSelectionPos();
    //return false;
    if(selectionPos == null)
    	return;
    var fragment = {};
    fragment.startPos = selectionPos.startPos;
    fragment.endPos = selectionPos.endPos;
    fragment.content = selectionPos.strContent;
    fragment.colorIndex = colorIndex;

    debug("[hypertopicOverlay.js::doHighlightMenuClick] selection:" + dump(fragment),3);
    sidebarWindow.sidebar.doHighlight(fragment);

    /*if(res){
			sidebarWindow.sidebar.showFragments();
			sidebarWindow.sidebar.showFragmentsInViewpoints();
			sidebarWindow.highlighter.doFormatHtml();
		}*/
	}
}